package com.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.entity.Cartmessage;
import com.csu.mypetstore.api.entity.Item;
import com.csu.mypetstore.api.entity.ItemInventory;
import com.csu.mypetstore.api.persistence.CartmessageMapper;
import com.csu.mypetstore.api.persistence.ItemInventoryMapper;
import com.csu.mypetstore.api.persistence.ItemMapper;
import com.csu.mypetstore.api.persistence.ProductMapper;
import com.csu.mypetstore.api.service.CartService;
import com.csu.mypetstore.api.vo.CartmessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("cartService")
public class CartServiceImpl implements CartService {

    @Autowired
    CartmessageMapper cartmessageMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ItemInventoryMapper itemInventoryMapper;



    @Override
    public CommonResponse<List<CartmessageVO>> getCartByUsername(String username) {
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Cartmessage> cartmessageList = cartmessageMapper.selectList(queryWrapper);

        if(cartmessageList.isEmpty()){
            return CommonResponse.createForError("购物车为空");
        }
        List<CartmessageVO> cartVOList = new ArrayList<CartmessageVO>();
        for (Cartmessage cartmessage : cartmessageList) {
            Item item = itemMapper.selectById(cartmessage.getItemId());
            cartVOList.add(cartmessageToCartmessageVO(cartmessage,item));
        }
        return CommonResponse.createForSuccess(cartVOList);
    }


    @Override
    public List<CartmessageVO> getCartByUsername1(String username) {
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Cartmessage> cartmessageList = cartmessageMapper.selectList(queryWrapper);
        List<CartmessageVO> cartVOList = new ArrayList<CartmessageVO>();
        for (Cartmessage cartmessage : cartmessageList) {
            Item item = itemMapper.selectById(cartmessage.getItemId());
            cartVOList.add(cartmessageToCartmessageVO(cartmessage,item));
        }
        return cartVOList;
    }


    @Override
    public CartmessageVO cartmessageToCartmessageVO(Cartmessage cartmessage,Item item){
        CartmessageVO cartmessageVO = new CartmessageVO();
        cartmessageVO.setCartId(cartmessage.getCartId());
        cartmessageVO.setUsername(cartmessage.getUsername());
        cartmessageVO.setItemId(cartmessage.getItemId());
        cartmessageVO.setQuantity(cartmessage.getNumber());
        cartmessageVO.setListPrice(item.getListPrice());
        ItemInventory itemInventory= itemInventoryMapper.selectById(item.getItemId());
        if(itemInventory.getQuantity()>0){
            cartmessageVO.setInStock(true);
        }else{
            cartmessageVO.setInStock(false);
        }
        BigDecimal listPrice = item.getListPrice();
        BigDecimal number = new BigDecimal(cartmessage.getNumber());
        cartmessageVO.setTotal(listPrice.multiply(number));
        cartmessageVO.setProductId(item.getProductId());
        cartmessageVO.setItem(item);
        return cartmessageVO;
    }

    @Override
    public CommonResponse<Boolean> removeCartByCartId(String CartId){
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CartId",CartId);

        int a = cartmessageMapper.delete(queryWrapper);
        if(a==1){
            return CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForError("删除失败");
    }

    @Override
    public CommonResponse<Boolean> removeCartByCartId1(int CartId){
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CartId",CartId);

        int a = cartmessageMapper.delete(queryWrapper);
        if(a==1){
            return CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForError("删除失败");
    }


    @Override
    public Cartmessage selectCartItemByCartId(String cartId){
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cartId",cartId);
        Cartmessage cartmessage = cartmessageMapper.selectOne(queryWrapper);
        return cartmessage;
    }

    @Override
    public CommonResponse<Boolean> insertCart(CartmessageVO cartmessageVO) {
        Cartmessage cartmessage = cartItemVOToCartItem(cartmessageVO);
        int a = cartmessageMapper.insert(cartmessage);
        if(a==1){
            return CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForError("插入失败");
    }

    @Override
    public CommonResponse<Boolean> removeCart(CartmessageVO cartmessageVO) {
        Cartmessage cartmessage = cartItemVOToCartItem(cartmessageVO);
        QueryWrapper<Cartmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemId",cartmessage.getItemId());
        int a = cartmessageMapper.delete(queryWrapper);
        if(a==1){
            return CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForError("删除失败");
    }

    @Override
    public CommonResponse<Boolean> updateCart(CartmessageVO cartmessageVO) {
        Cartmessage cartmessage = cartItemVOToCartItem(cartmessageVO);
        int a = cartmessageMapper.updateById(cartmessage);

        if(a==1){
            return CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForError("更新失败");
    }

    public Cartmessage cartItemVOToCartItem(CartmessageVO cartmessageVO){
        Cartmessage cartmessage = new Cartmessage();
        cartmessage.setCartId(cartmessageVO.getCartId());
        cartmessage.setItemId(cartmessageVO.getItemId());
        cartmessage.setUsername(cartmessageVO.getUsername());
        cartmessage.setNumber(cartmessageVO.getQuantity());
        return cartmessage;
    }

    @Override
    public Item selectItemById(String itemID){
        Item item = itemMapper.selectById(itemID);
        return item;
    }
}

